<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
<<<<<<< HEAD
use app\admin\model\Classifys;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
=======

>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
class Classify extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
<<<<<<< HEAD
        $where=[];
        $where['pid']=0;
        $where['status']=1;
        $classify=new Classifys();
        $list=$classify->index($where);
        $data=$list->items();
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();
    }

=======
        //
        $where = [];
        $where['pid'] = 0;
        $where['status']=1;
        $list = db( "classify" )->where( $where )->paginate(5);
        $data = $list->items();
        $this->assign("list",$list);
        $this->assign("data",$data);
        return $this->fetch();
    }
    public function classify_index()
    {
        $where = [];
        $where['status']=1;
        $list = db( "classify" )->where( $where )->paginate(5);
        $data = $list->items();
        $this->assign("list",$list);
        $this->assign("data",$data);
        return $this->fetch();
    }
>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
<<<<<<< HEAD
          return $this->fetch();
=======
        //
        return $this->fetch();
>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
<<<<<<< HEAD
    public function save()
    {
        $data=input();
        $classify=new Classifys();
        $res=$classify->add($data);
        if ($res){
                return $this->success('添加成功',url('Classify/index'));
        }else{
                return $this->error('添加失败');
        }
    }
    public function add()
    {
        if($this->request->isPost()){
            $data=input();
            $classify=new Classifys();
            $res=$classify->add($data);
            if ($res){
                    return $this->success('添加成功',url('Classify/update',['id'=>$data['pid']]));;
            }else{
                    return $this->error('添加失败');
            }
        }else{
            $list=db('classify')->where('pid',0)->select();
            $this->assign('list',$list);
            return $this->fetch();
        }
       
    }
    public function reades()
    {
        if($this->request->isPost()){
               $data=input();
                $classify=new Classifys();
                $res=$classify->edit($data);
                if ($res){
                        return $this->success('编辑成功',url('Classify/update',['id'=>$data['pid']]));
                }else{
                        return $this->error('编辑失败');
                }
        }else{
            $id=input('id');
            $data=db('classify')->where('id',$id)->find();
            $list=db('classify')->where('pid',0)->select();
            $this->assign('list',$list);
             $this->assign('data',$data);
            return $this->fetch();
        }
       
    }
    public  function upload()
     {
         $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = '6IeVuqWlzQ9f3Vz19UVOAbDDxmwjZpZdf8VId5F_';
        $secretKey = '5T3TJ_O0x1rUOGtaw4kktV91nYZqe-NefC1dACk-';
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = '1810php';

        //域名
        $domain = 'qutzb4nvh.hb-bkt.clouddn.com';
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }
       
     }
=======
    public function save(Request $request)
    {
        //
        $inputdata=input();
        $res=db('classify')->insert($inputdata);
        if($res){
            $this->success("添加成功",url("classify/index"));
        }else{
            $this->error("修改失败");
        }
    }

>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
<<<<<<< HEAD
        $data=db('classify')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
=======
        //
>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
<<<<<<< HEAD
    public function edit()
    {
        $data=input();
        $classify=new Classifys();
        $res=$classify->edit($data);
        if ($res){
                return $this->success('编辑成功',url('Classify/index'));
        }else{
                return $this->error('编辑失败');
        }
=======
    public function edit($id)
    {
        //
        $find=db('classify')->where(['id'=>$id])->find();
        $where=array();
        $this->assign("find",$find);
        return $this->fetch();
>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
<<<<<<< HEAD
    public function update()
    {
        $pid=input('id');
        $where=[];
        $where['pid']=$pid;
        $where['status']=1;
        $list=db('classify')->where($where)->paginate(2);
        $data=$list->items();
        foreach ($data as $key => $value) {
           $data[$key]['pid']=db('classify')->where('id',$value['pid'])->value('name');
        }
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();

=======
    public function update(Request $request, $id)
    {
        //
        $inputdata=input();
        $res=db('classify')->where(['id'=>$id])->update($inputdata);
        if($res){
            $this->success("修改成功",url("classify/index"));
        }else{
            $this->error("修改失败");
        }
>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
<<<<<<< HEAD
        $res=db('classify')->where('id',$id)->delete();
        if ($res){
                return $this->success('删除成功',url('Classify/update',['id'=>$id]));
        }else{
                return $this->error('删除失败');
        }
    }
    public function remove($id)
    {
        $res=db('classify')->where('id',$id)->delete();
        if ($res){
                return $this->success('删除成功',url('Classify/index'));
        }else{
                return $this->error('删除失败');
        }
    }
=======
        //
    }
    public function remove($id)
    {
        $id= input('id');
        if (db('classify')->where('id',$id)->delete()) {
            $this->success('删除成功',url("video/fodder"));
        } else {
            $this->error('删除失败');
        }
    }

>>>>>>> 244d0891c8a5b18fb59730fa4df1787085ccf662
}
